package com.sw.bbs.common.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * DateFormatUtil
 *
 * @author Johnathan
 **/
public class DateFormatUtil {

    public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_PATTERN_1 = "yyyy-MM-dd";
    public static final String DATE_PATTERN_2 = "yyyyMMdd";
    public static final String DATE_PATTERN_3 = "yyyy.MM.dd";
    public static final String DATE_PATTERN_4 = "yyyy.MM.dd HH:mm:ss";
    public static final String DATE_PATTERN_5 = "HHmmss";
    public static final String DATE_PATTERN_Y = "yyyy";
    public static final String DATE_PATTERN_YM = "yyyy-MM";
    public static final String DATE_PATTERN_YMDH = "yyyy-MM-dd HH";
    public static final String DATE_PATTERN_YMDHSS = "yyyy-MM-dd HH:mm:ss.SSS";
    
    /**
     * 格式化时间
     * RFC 3339格式
     * @param date
     * @return
     */
    public static String getUTCDateFormat(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        return simpleDateFormat.format(date);
    }
    
    /**
     * 按指定格式格式化时间
     * @param date
     * @param pattern default "yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static String getDateFormat(Date date, String pattern) {
        String dateStr = "";
        if (pattern == null) {
            pattern = DEFAULT_FORMAT;
        }
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
            dateStr = simpleDateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateStr;
    }

    /**
     * 时间字符串转换成Date
     * @param date
     * @param format
     * @return         
     * @throws
     */
    public static Date parseStringToDate(String date, String format) {
        Date result;
        if (format == null) {
            format = DEFAULT_FORMAT;
        }
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            result = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            return null;
        }
        return result;
    }
    
    public static String getLocalDateFormat(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DEFAULT_FORMAT);
        return simpleDateFormat.format(date);
    }
    
    public static String getLocalDateFormat(Date date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    /**
     * utc时间转成local时间
     * @param utcTime yyyy-MM-ddTHH:mm:ssZ
     * @return
     */
    public static Date utcToLocal(String utcTime){
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .appendPattern("yyyy-MM-dd").appendLiteral('T')
                .appendPattern("HH:mm:ss")
                .optionalStart()
                .appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true)
                .optionalEnd()
                .appendLiteral('Z')
                .toFormatter();
        LocalDateTime ldt;
        try {
            ldt = LocalDateTime.parse(utcTime, formatter);
        } catch (Exception e) {
            return null;
        }
        ZonedDateTime zdt = ldt.atZone(ZoneId.of("UTC"));
        Date date = Date.from(zdt.toInstant());
        return date;
    }
    
    
    /**
     * local时间转换成UTC时间
     * @param localTime yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String localToUTC(String localTime) {
        Date localDate = parseStringToDate(localTime, null);
        long localTimeInMillis=localDate.getTime();
        /** long时间转换成Calendar */
        Calendar calendar= Calendar.getInstance();
        calendar.setTimeInMillis(localTimeInMillis);
        /** 取得时间偏移量 */
        int zoneOffset = calendar.get(Calendar.ZONE_OFFSET);
        /** 取得夏令时差 */
        int dstOffset = calendar.get(Calendar.DST_OFFSET);
        /** 从本地时间里扣除这些差量，即可以取得UTC时间*/
        calendar.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
        /** 取得的时间就是UTC标准时间 */
        Date utcDate=new Date(calendar.getTimeInMillis());
        return getLocalDateFormat(utcDate, "yyyy-MM-dd'T'HH:mm:ss'Z'");
    }

    /**
     * 把long 转换成 日期 再转换成String类型
     */
    public Long transferLongToDate(String dateFormat, Long millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date date = new Date(millSec);
        return date.getTime();
    }
    
    public static void main(String[] args) throws ParseException {
        /*SearchObjectIndentifier objectIndentifier = JacksonUtil.readObjectFromJSON("{\"sequence\":0,\"captured_time\":\"2018-12-09T07:37:52.225Z\"}", SearchObjectIndentifier.class);

        System.out.println(JacksonUtil.writeObjectToPerttyJSON(objectIndentifier));
        System.out.println(objectIndentifier.getCapturedTime().getTime());

        TimeRange period = new TimeRange("2018-12-01T00:00:00.000Z", "2050-01-01T23:59:59.000Z");
        System.out.println(period.getStart());
        System.out.println(period.getEnd());
        System.out.println(JacksonUtil.writeObjectToPerttyJSON(period));*/
        
        System.out.println(getUTCDateFormat(new Date()));
        
    }
}
